package com.blog.attach.controller;


import com.blog.attach.service.UserCommentService;
import com.blog.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 苏离
 * @since 2022-01-18
 */
@RestController
@CrossOrigin("*")
@RequestMapping("/attach/userComment")
public class UserCommentController {
    @Autowired
    UserCommentService commentService;

    /**
     * 查询当前文章所有的评论
     * @param id 文章id
     * @param limit 最大查询条数
     * @param pageNo 当前页数
     * @return 评论详情
     */
    @GetMapping("/getComments/{id}/{pageNo}/{limit}")
    public ResponseResult getCommentByArticleId(@PathVariable Integer id,
                                                @PathVariable Integer limit,
                                                @PathVariable Integer pageNo){
    Map<String, Object> comments = commentService.getComments(id, pageNo, limit);
    return ResponseResult.ok().data(comments);
}
    @PostMapping("/insertComments/{userId}/{articleId}")
    public ResponseResult insertComment(@PathVariable Integer articleId,
                                        @PathVariable String userId,
                                        @RequestBody Map<String,String> map){
        String commentContent = map.get("commentContent");
        if(commentService.insertComment(userId, articleId, commentContent))
        return ResponseResult.ok().message("发表评论成功");
        return ResponseResult.error().message("评论失败，请检查您的网络");
    }
    @DeleteMapping("/deleteComments/{commentId}")
    public ResponseResult deleteComment(@PathVariable Integer commentId){

        if(commentService.deleteComment(commentId))
            return ResponseResult.ok().message("删除评论成功");
        return ResponseResult.error().message("删除评论失败，请检查您的网络");
    }
}

